﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using server.Model;
using System.Diagnostics;

namespace server
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 真实身份转换
        /// </summary>
        /// <param name="RID">车辆车牌号</param>
        /// <returns>string转Element转string</returns>
        private string Trans(string RID)
        {
            Process LoginProcess = new Process();
            string filename = "Login.exe";
            string para = "a.param " + RID.Remove(0, 1);
            //string RID_1 = RID.Remove(0, 2);
            ProcessStartInfo Info = new ProcessStartInfo(filename, para);
            Info.UseShellExecute = false;
            Info.RedirectStandardOutput = true;
            Info.WindowStyle = ProcessWindowStyle.Hidden;
            Info.CreateNoWindow = true;
            LoginProcess.StartInfo = Info;
            LoginProcess.Start();
            while (!LoginProcess.HasExited)
            {
                LoginProcess.WaitForExit();
            }
            string value = LoginProcess.StandardOutput.ReadToEnd();
            LoginProcess.Close();
            return value;
        }

        public bool flag = false;
        VanetRID VR = null;
        private void lpnumbut_Click(object sender, EventArgs e)
        {
            VanetRIDal VRD = new VanetRIDal();
            VR = new VanetRID();

            VR.RID =lpnumtb.Text.ToString();
            VR.RID_trans = Trans(VR.RID);
            
            flag = VRD.Create(VR);
            if (flag)
            {
                MessageBox.Show("注册成功！");
            }
            else
            {
                MessageBox.Show("请不要重复注册！");
            }

            this.Close();
        }

        private void lpnumtb_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void lpnumtb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                VanetRIDal VRD = new VanetRIDal();
                VR = new VanetRID();

                VR.RID = lpnumtb.Text.ToString();
                VR.RID_trans = Trans(VR.RID);

                flag = VRD.Create(VR);
                if (flag)
                {
                    MessageBox.Show("注册成功！");
                }
                else
                {
                    MessageBox.Show("请不要重复注册！");
                }

                this.Close();
            }
        }
    }
}
